
package com.etrans.web.gps.business.report.controller;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.AreaAnalyseCarType;
import com.etrans.web.gps.business.report.domain.AreaAnalyzesVo;
import com.etrans.web.gps.business.report.service.AreaAnalyzesService;
import com.etrans.web.gps.business.system.service.CommonService;

/**
 * @author RenweiWu
 * @date 2017年8月30日
 * @ClassName:  PlatformAnalyzesController  
 * @param:   
 */
@Controller
@RequestMapping("/base/report/AreaAnalyzes/")
public class AreaAnalyzesController extends BaseController{
	
	@Autowired
	private AreaAnalyzesService areaAnalyzesService;
	@Autowired
	public CommonService commonService;
	
	@RequestMapping("toAreaAnalyzes.html")
	public String toAreaAnalyzes(){
		return "views/business/report/areaanalyzes";
	}
	//base/report/AreaAnalyzes/pageToAreaAnalyzes.html?pageAreaId=2741
	@RequestMapping("pageToAreaAnalyzes.html")
	public String homepageToAreaAnalyzesQC(String pageAreaId, String pageVeidoo, String pageAnalyse_date, Model model){
		model.addAttribute("pageAreaId",pageAreaId);
		model.addAttribute("pageVeidoo",pageVeidoo);
		model.addAttribute("pageAnalyse_date",pageAnalyse_date);
		return "views/business/report/areaanalyzes";
	}
	@RequestMapping("toAreaAnalyzesAL.html")
	public String toAreaAnalyzesAL(String pageAreaId, String pageVeidoo, String pageAnalyse_date, Model model){
		model.addAttribute("pageAreaId",pageAreaId);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("pageAreaId", pageAreaId);
		String pageAreaName = areaAnalyzesService.queryAreaName(map);
		if(pageAreaId!=null && !"".equals(pageAreaId)){
			Integer pageAreaLevel = commonService.findAreaLevelById(Integer.parseInt(pageAreaId));
			model.addAttribute("pageAreaLevel",pageAreaLevel);
		}
		model.addAttribute("pageAreaName",pageAreaName);
		model.addAttribute("pageVeidoo",pageVeidoo);
		model.addAttribute("pageAnalyse_date",pageAnalyse_date);
		return "views/business/report/areaanalyzesAL";
	}
	@RequestMapping("toAreaAnalyzesQC.html")
	public String toAreaAnalyzesQC(){
		return "views/business/report/areaanalyzesQC";
	}
	
	/**
	 * 区域分析
	 * @param areaName
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("queryAreaAnalyzes.html")
	@ResponseBody
	public Map<String,Object> queryAreaAnalyzes(String areaId,@RequestParam(value="areaIds[]",required=false)Integer[] areaIds,String level, String veidoo, String analyse_date){
		Integer area_id = this.getSysUser().getArea_id();
		Map<String,Object> list=null;
		try {
			 list = areaAnalyzesService.queryAnalyzes(areaId, areaIds, level, veidoo, analyse_date,area_id);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	@RequestMapping("showShadowRegion.html")
	public @ResponseBody List<AreaAnalyzesVo>  showShadowRegion(String area_id, String veidoo, String analyse_date){
		List<AreaAnalyzesVo> list = areaAnalyzesService.getShadowRegion(area_id,veidoo,analyse_date);
		return list;
	}
	
	
	
	/**
	 * 区域考核导出
	 * @param request
	 * @param response
	 * @param title
	 * @param areaIds
	 * @param veidoo
	 * @param analyse_date
	 * @param colModel
	 * @param imgsrc
	 * @throws ParseException
	 */
	@RequestMapping(value = "exportAreaAssessment.html")
	@ResponseBody
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void exportAreaAssessment( HttpServletRequest request, HttpServletResponse response, 
			String title,
			//@RequestParam(value="areaIds[]",required=false)Integer[] areaIds,
			Integer[] areaIds,
			String level,  String veidoo, String analyse_date, String colModel) throws ParseException {
		DecimalFormat DFS = new DecimalFormat("0.00");
		//获取参数
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		Integer area_id = this.getSysUser().getArea_id();
		Map<String, Object> queryAnalyzes = areaAnalyzesService.queryAnalyzes(null, areaIds,level, veidoo, analyse_date,area_id);
		List<AreaAnalyzesVo> listAnalyzes=(List<AreaAnalyzesVo>) queryAnalyzes.get("listAnalyzes");
		for (AreaAnalyzesVo vo : listAnalyzes) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ranking", vo.getRanking());
			map.put("score", vo.getScore());
			map.put("area_name", vo.getArea_name());
			map.put("vehicle_access_rate", vo.getVehicle_access_rate());
			map.put("vehicle_online_rate", vo.getVehicle_online_rate());
			map.put("reply_rate", vo.getReply_rate());
			map.put("track_complete_rate", vo.getTrack_complete_rate());
			map.put("data_qualified_rate",(vo.getTrack_normal()+vo.getException_track_count())>0?DFS.format((1-((double)vo.getException_track_count()/(vo.getTrack_normal()+vo.getException_track_count())))*100):0);
			map.put("adas_alarm_count", vo.getAdas_alarm_count());
			rows.add(map);
		}
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,-1,-1),rowsList,null);
	}
	
	/**
	 * 区域综合分析导出
	 * @param request
	 * @param response
	 * @param title
	 * @param areaIds
	 * @param veidoo
	 * @param analyse_date
	 * @param colModel
	 * @throws ParseException
	 */
	@RequestMapping(value = "exportAreaAnalyzes.html")
	@ResponseBody
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void exportAreaAnalyzes( HttpServletRequest request, HttpServletResponse response, 
			String title, Integer[] areaIds, String level,  String veidoo, String analyse_date, 
			String colModel, String imgsrc, String sheetNames) throws ParseException {
		DecimalFormat DFS = new DecimalFormat("0.00");
		//获取参数
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
		List<String> sheetList   = JSON.parseObject(sheetNames, List.class);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> rows2 = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> rows3 = new ArrayList<HashMap<String, Object>>();
		List<HashMap<String, Object>> rows4 = new ArrayList<HashMap<String, Object>>();
		Integer area_id = this.getSysUser().getArea_id();
		Map<String, Object> queryAnalyzes = areaAnalyzesService.queryAnalyzes(null, areaIds, level, veidoo, analyse_date,area_id);
		List<AreaAnalyzesVo> listAnalyzes=(List<AreaAnalyzesVo>) queryAnalyzes.get("listAnalyzes");
		for (AreaAnalyzesVo vo : listAnalyzes) {
			HashMap<String, Object> map1 = new HashMap<String, Object>();
			HashMap<String, Object> map3 = new HashMap<String, Object>();
			HashMap<String, Object> map4 = new HashMap<String, Object>();
			
			String area_name=vo.getArea_name();
			
			map1.put("area_name", area_name);
			map1.put("vehicle_count", vo.getVehicle_count());
			map1.put("vehicle_access_count", vo.getVehicle_access_count());
			map1.put("vehicle_access_rate", vo.getVehicle_access_rate());
			map1.put("vehicle_online_count", vo.getVehicle_online_count());
			map1.put("vehicle_online_rate", vo.getVehicle_online_rate());
			rows.add(map1);
			
			map3.put("area_name", area_name);
			map3.put("alarm_vehicle_count", vo.getAlarm_vehicle_count());
			map3.put("alert_rate", vo.getVehicle_access_count()>0?DFS.format(((double)vo.getAlarm_vehicle_count()/vo.getVehicle_access_count())*100):"0");
			map3.put("alarm_count", vo.getAlarm_count());
			map3.put("alarm_processed", vo.getAlarm_processed());
			map3.put("alarm_processed_rate", vo.getAlarm_count()>0?DFS.format(((double)vo.getAlarm_processed()/vo.getAlarm_count())*100):"0");
			map3.put("overspeed_count", vo.getOverspeed_count());
			map3.put("overspeed_count_avg",vo.getVehicle_online_count()>0?DFS.format(((double)vo.getOverspeed_count()/vo.getVehicle_online_count())):"0");
			map3.put("tired_duration", vo.getTired_duration()/60);
			map3.put("outTime", vo.getVehicle_online_count()>0?DFS.format(((1.0*vo.getTired_duration()/60)/vo.getVehicle_online_count())):"0");
			rows3.add(map3);
			
			map4.put("area_name", area_name);
			map4.put("exception_track_count", vo.getException_track_count());
			map4.put("exception_rate",vo.getVehicle_online_count()>0?DFS.format(((double)vo.getException_vehicle_count()/vo.getVehicle_online_count())*100):"0");
			map4.put("exception_vehicle_count",vo.getException_vehicle_count() );
			map4.put("speed_160_count", vo.getSpeed_160_count());
			map4.put("location_exception_count", vo.getLocation_exception_count());
			map4.put("gpstime_exception_count", vo.getGpstime_exception_count());
			map4.put("head_exception_count", vo.getHead_exception_count());
			map4.put("exception_contrast_rate",vo.getTrack_normal()+vo.getException_track_count()>0?DFS.format(((double)vo.getException_track_count()/(vo.getTrack_normal()+vo.getException_track_count()))*100):"0");
			rows4.add(map4);
			
		}
		//图表
		List<AreaAnalyseCarType> list = (List<AreaAnalyseCarType>) queryAnalyzes.get("list");
		for (AreaAnalyseCarType vo : list) {
			HashMap<String, Object> map2 = new HashMap<String, Object>();
			map2.put("areaName", vo.getAreaName());
			map2.put("regularBus", vo.getRegularBus());
			map2.put("touringCar", vo.getTouringCar());
			map2.put("dangerCar", vo.getDangerCar());
			rows2.add(map2);
		}
		
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		rowsList.add(rows2);
		rowsList.add(rows3);
		rowsList.add(rows4);
		exportExls(request, response,title, removeHeadName(colModelList,-1,-1,-1,-1),rowsList,imgsrcList, sheetList);
	}
	
}
